﻿using System;
using System.Collections.Generic;
using Draco.OWF.Logs;
using Draco.OWF.Metadata.DataBinding;
using Draco.OWF.RT.Impl;
using Draco.OWF.Metadata;

namespace Draco.OWF.RT.Engine.Worker
{
    public class WorkerEngine : IWorkerEngine
    {
        protected static ILogs m_logger = new Logger();

        protected bool m_Running=false;
        protected IJob job=null;
        protected IJobContext jobContext = null;

        #region constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="job"></param>
        public WorkerEngine(IJob job)
        {
            if (job == null)
                throw new ArgumentNullException("processInstance is null");
            this.job = job;
        }
        #endregion

        #region public method
        /// <summary>
        /// Run process
        /// </summary>
        public virtual void Start(Object startParameters)
        {
            m_logger.Info("start to run Process instance:" + this.job.Id);
            BindingData arguments = startParameters as BindingData;
            if (this.job.Status == JobStatus.Completed)
            {
                m_logger.Info("Process instance had stopped");
                return;
            }

            m_Running = true;
            IList<ITask> lastTasks = null;
            while (m_Running)
            {
                lastTasks = this.job.MoveNext(this.jobContext, lastTasks);
                if(this.job.Status == JobStatus.Completed)
                    m_Running =false;
            }
        }
        /// <summary>
        /// Stop process
        /// </summary>
        public virtual void Stop()
        {
            this.m_Running = false;
        }
        #endregion
    }
}
